package com.guli.edu.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.guli.edu.entity.Subject;
import com.guli.edu.entity.Teacher;
import com.guli.edu.entity.excel.SubjectData;
import com.guli.edu.entity.subject.OneSubject;
import com.guli.edu.entity.subject.TwoSubject;
import com.guli.edu.listener.SubjectExcelListener;
import com.guli.edu.mapper.SubjectMapper;
import com.guli.edu.service.SubjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author liuzg
 * @since 2021-03-05
 */
@Service
public class SubjectServiceImpl extends ServiceImpl<SubjectMapper, Subject> implements SubjectService {

    @Override
    public void saveSubject(MultipartFile file, SubjectService subjectService) {
        try {
            //文件输入流
            InputStream in = file.getInputStream();
            //调用方法进行读取
            EasyExcel.read(in, SubjectData.class,new SubjectExcelListener(subjectService)).sheet().doRead();
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public List<OneSubject> getAllOneTwoSubject() {
        // 查出所有一级分类
        QueryWrapper<Subject> oneQueryWrapper = new QueryWrapper<>();
        oneQueryWrapper.eq("parent_id","0");
        List<Subject> oneSubjectList = baseMapper.selectList(oneQueryWrapper);

        // 查询所有二级分类
        QueryWrapper<Subject> twoQueryWrapper = new QueryWrapper<>();
        twoQueryWrapper.ne("parent_id","0");
        List<Subject> twoSubjectList = baseMapper.selectList(twoQueryWrapper);

        // 最终返回的课程list
        List<OneSubject> finalSubjectList = new ArrayList<>();

        // 3 封装一级分类
        // 查询出来所有的一级分类list集合遍历，得到每个一级分类对象，获取每个一级分类对象值，
        // 封装到要求的list集合里面 List<OneSubject> finalSubjectList
        for (int i = 0; i < oneSubjectList.size(); i++) {

            // 得到oneSubjectList每个eduSubject对象
            Subject subject = oneSubjectList.get(i);

            OneSubject oneSubject = new OneSubject();
            // 把eduSubject里面值获取出来，放到OneSubject对象里面
            // Subject值复制到对应oneSubject对象里面
            BeanUtils.copyProperties(subject,oneSubject);

            // 多个OneSubject放到finalSubjectList里面
            finalSubjectList.add(oneSubject);

            // 在一级分类循环遍历查询所有的二级分类
            // 创建list集合封装每个一级分类的二级分类
            List<TwoSubject> twoFinalSubjectsList = new ArrayList<>();

            // 遍历二级分类list集合
            for (int m = 0; m < twoSubjectList.size(); m++) {
                // 获取每个二级分类
                Subject tSubject = twoSubjectList.get(m);

                // 判断二级分类parentid和一级分类id是否一样
                if (tSubject.getParentId().equals(subject.getId())){
                    // 把tSubject值复制到TwoSubject里面，放到twoFinalSubjectList里面
                    TwoSubject twoSubject = new TwoSubject();
                    BeanUtils.copyProperties(tSubject,twoSubject);
                    twoFinalSubjectsList.add(twoSubject);
                }
            }

            oneSubject.setChildren(twoFinalSubjectsList);
        }

        return finalSubjectList;
    }

}
